﻿using System.Data.Objects;
using System.Data.Objects.DataClasses;

namespace hieunv.MVC3.App.Models
{
    public interface IDataAccess
    {
        ObjectContext Context { get; set; }
    }
    public static class DataAccess
    {
        public static bool Insert<T>(this T x, bool save) where T : class, IDataAccess
        {
            x.Context.CreateObjectSet<T>().AddObject(x);
            return (save) ? x.Context.SaveChanges() > 0 : true;
        }
        public static bool Insert<T>(this T x) where T : class, IDataAccess
        {
            return Insert<T>(x, true);
        }
        public static bool Update<T>(this T x) where T : class, IDataAccess
        {
            return x.Context.SaveChanges() > 0;
        }
        public static bool Delete<T>(this T x, bool save) where T : class, IDataAccess
        {
            x.Context.CreateObjectSet<T>().DeleteObject(x);
            return (save) ? x.Context.SaveChanges() > 0 : true;
        }
        public static bool Delete<T>(this T x) where T : class, IDataAccess
        {
            return Delete<T>(x, true);
        }
    }
}